package br.leiam.eolica.denis;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;

public class AcessoBD{
    private Connection connection;  //conex�o com o banco de dados
    private Statement statement;    //cria statement
    private boolean conectado=false;
    
    public AcessoBD(String driver,String database_url,String username,String password)
    {
        try
        {
            Class.forName(driver);  //carrega o driver na mem�ria
            connection = DriverManager.getConnection(database_url, username, password);
            statement = connection.createStatement();
            conectado=true;
        }
        catch (SQLException sqlException)
        {
                 sqlException.printStackTrace();
                 conectado=false;
        }
         catch (ClassNotFoundException classNotFound)
         {
          classNotFound.printStackTrace();
          conectado=false;
         }
    }
    public void desconecta()    //rotina para desconectar do banco de dados
    {
        if(!conectado)return;
        
        try
        {
            statement.close();
            connection.close();
        }
        catch(SQLException sqlException)
        {
            sqlException.printStackTrace();
        }
        finally
        {
            conectado=false;
        }
    }
    public void executar(String comando)//comandos sql
    {
        try
        {
        statement.execute(comando);
        }
        catch(SQLException sqlException)
        {
            sqlException.printStackTrace();
        }
    }

}
